package com.jintu.portal.utils;

import com.jintu.portal.pojo.exception.CryptoException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author very
 */
@Slf4j
public class MD5Helper {

    public static String md5Encode(String username, Long timestamp, String userKey, String orderId) throws CryptoException {
        if(StringUtils.isBlank(username) || timestamp == null || StringUtils.isBlank(userKey) || StringUtils.isBlank(orderId)){
            log.error("md5加密信息时，用户username,时间戳,用户userKey和订单号均不能为空");
            throw new CryptoException("md5加密信息时，用户username,时间戳,用户userKey和订单号均不能为空");
        }
        String src = username + "-" + timestamp + "-" + userKey + "-" + orderId;
        try {
            return DigestUtils.md5Hex(src);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CryptoException("md5加密信息时，md5加密异常， " + e.getMessage());
        }
    }

    public static String md5EncodeOverdue(String username, Long timestamp, String userKey) throws CryptoException {
        if(StringUtils.isBlank(username) || timestamp == null || StringUtils.isBlank(userKey)){
            log.error("md5加密信息时，用户username,时间戳,用户userKey均不能为空");
            throw new CryptoException("md5加密信息时，用户username,时间戳,用户userKey和订单号均不能为空");
        }
        String src = username + "-" + timestamp + "-" + userKey;
        try {
            return DigestUtils.md5Hex(src);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CryptoException("md5加密信息时，md5加密异常， " + e.getMessage());
        }
    }


    public static void main(String[] args) throws CryptoException {
        System.out.println(md5Encode("admin", 1111L, "c8e9172f341d4be6ae77f2cdc9431e14", "order-3"));
    }
}
